package org.jivesoftware.smackx.bookmark;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.PrivateDataManager;

public class BookmarkManager
{
  private static final Map<Connection, BookmarkManager> bookmarkManagerMap = new HashMap();
  private final Object bookmarkLock = new Object();
  private Bookmarks bookmarks;
  private PrivateDataManager privateDataManager;

  static
  {
    PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks", new Bookmarks.Provider());
  }

  private BookmarkManager(Connection paramConnection)
    throws XMPPException
  {
    if ((paramConnection == null) || (!(paramConnection.isAuthenticated())))
      throw new XMPPException("Invalid connection.");
    this.privateDataManager = new PrivateDataManager(paramConnection);
  }

  /**
   * @deprecated
   */
  public static BookmarkManager getBookmarkManager(Connection paramConnection)
    throws XMPPException
  {
    monitorenter;
    try
    {
      BookmarkManager localBookmarkManager = (BookmarkManager)bookmarkManagerMap.get(paramConnection);
      if (localBookmarkManager == null)
      {
        localBookmarkManager = new BookmarkManager(paramConnection);
        bookmarkManagerMap.put(paramConnection, localBookmarkManager);
      }
      monitorexit;
      return localBookmarkManager;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }

  private Bookmarks retrieveBookmarks()
    throws XMPPException
  {
    synchronized (this.bookmarkLock)
    {
      if (this.bookmarks == null)
        this.bookmarks = ((Bookmarks)this.privateDataManager.getPrivateData("storage", "storage:bookmarks"));
      Bookmarks localBookmarks = this.bookmarks;
      return localBookmarks;
    }
  }

  public void addBookmarkedConference(String paramString1, String paramString2, boolean paramBoolean, String paramString3, String paramString4)
    throws XMPPException
  {
    retrieveBookmarks();
    BookmarkedConference localBookmarkedConference1 = new BookmarkedConference(paramString1, paramString2, paramBoolean, paramString3, paramString4);
    List localList = this.bookmarks.getBookmarkedConferences();
    if (localList.contains(localBookmarkedConference1))
    {
      BookmarkedConference localBookmarkedConference2 = (BookmarkedConference)localList.get(localList.indexOf(localBookmarkedConference1));
      if (localBookmarkedConference2.isShared())
        throw new IllegalArgumentException("Cannot modify shared bookmark");
      localBookmarkedConference2.setAutoJoin(paramBoolean);
      localBookmarkedConference2.setName(paramString1);
      localBookmarkedConference2.setNickname(paramString3);
      localBookmarkedConference2.setPassword(paramString4);
    }
    while (true)
    {
      this.privateDataManager.setPrivateData(this.bookmarks);
      return;
      this.bookmarks.addBookmarkedConference(localBookmarkedConference1);
    }
  }

  public void addBookmarkedURL(String paramString1, String paramString2, boolean paramBoolean)
    throws XMPPException
  {
    retrieveBookmarks();
    BookmarkedURL localBookmarkedURL1 = new BookmarkedURL(paramString1, paramString2, paramBoolean);
    List localList = this.bookmarks.getBookmarkedURLS();
    if (localList.contains(localBookmarkedURL1))
    {
      BookmarkedURL localBookmarkedURL2 = (BookmarkedURL)localList.get(localList.indexOf(localBookmarkedURL1));
      if (localBookmarkedURL2.isShared())
        throw new IllegalArgumentException("Cannot modify shared bookmarks");
      localBookmarkedURL2.setName(paramString2);
      localBookmarkedURL2.setRss(paramBoolean);
    }
    while (true)
    {
      this.privateDataManager.setPrivateData(this.bookmarks);
      return;
      this.bookmarks.addBookmarkedURL(localBookmarkedURL1);
    }
  }

  public Collection<BookmarkedConference> getBookmarkedConferences()
    throws XMPPException
  {
    retrieveBookmarks();
    return Collections.unmodifiableCollection(this.bookmarks.getBookmarkedConferences());
  }

  public Collection<BookmarkedURL> getBookmarkedURLs()
    throws XMPPException
  {
    retrieveBookmarks();
    return Collections.unmodifiableCollection(this.bookmarks.getBookmarkedURLS());
  }

  public void removeBookmarkedConference(String paramString)
    throws XMPPException
  {
    retrieveBookmarks();
    Iterator localIterator = this.bookmarks.getBookmarkedConferences().iterator();
    if (!(localIterator.hasNext()));
    while (true)
    {
      BookmarkedConference localBookmarkedConference;
      do
      {
        return;
        localBookmarkedConference = (BookmarkedConference)localIterator.next();
      }
      while (!(localBookmarkedConference.getJid().equalsIgnoreCase(paramString)));
      if (localBookmarkedConference.isShared())
        throw new IllegalArgumentException("Conference is shared and can't be removed");
      localIterator.remove();
      this.privateDataManager.setPrivateData(this.bookmarks);
    }
  }

  public void removeBookmarkedURL(String paramString)
    throws XMPPException
  {
    retrieveBookmarks();
    Iterator localIterator = this.bookmarks.getBookmarkedURLS().iterator();
    if (!(localIterator.hasNext()));
    while (true)
    {
      BookmarkedURL localBookmarkedURL;
      do
      {
        return;
        localBookmarkedURL = (BookmarkedURL)localIterator.next();
      }
      while (!(localBookmarkedURL.getURL().equalsIgnoreCase(paramString)));
      if (localBookmarkedURL.isShared())
        throw new IllegalArgumentException("Cannot delete a shared bookmark.");
      localIterator.remove();
      this.privateDataManager.setPrivateData(this.bookmarks);
    }
  }
}